import {observable, action, runInAction} from 'mobx';

/**
 * mobx 状态管理类，所有的逻辑处理都在这里
 * */
class RepairListStor {
    @observable hasMore; //是否是最后一页
    @observable refreshing; //是否下拉刷新
    @observable isLoading; //是否上拉刷新
    @observable repairData = []; //业主列表
    @observable segmentedControlIndex = 0;
    pageSize = 10;
    pageIndex = 1;

    @action
    onRefresh = () => {
        runInAction(() => {
            this.refreshing = true;
            this.isLoading = true;
        });
        this.pageIndex = 1;
        this.updateRepairList(this.pageIndex);
    };

    @action
    onEndReached = () => {
        if (this.isLoading || this.hasMore) {
            return;
        }
        this.isLoading = true;
        this.pageIndex = ++this.pageIndex;
        this.updateRepairList(this.pageIndex);

    };

    @action
    deleteRepair = (id) => {
        postJson("/comm/repair/deleteRepair.x", {'id': id}, () => {
            this.repairData = this.repairData.filter(x => x.id !== id);
        });
    };

    updateRepairList(pageIndex) {
        let params = {};
        params.page = pageIndex;
        params.pageSize = this.pageSize;
        params.commCode = commCode;
        params.state = this.segmentedControlIndex;
        postJson("/comm/repair/query.x", params, (data) => {
            runInAction(() => {
                this.hasMore = data.list.length < 10;
                if (pageIndex === 1) {
                    this.repairData = data.list;
                } else {
                    this.repairData = this.repairData.concat(data.list);
                }
                this.refreshing = false;
                this.isLoading = false;
            });
        }, (error) => {
            runInAction(() => {
                this.refreshing = false;
                this.isLoading = false;
            });
        });
    }

    onChangeSC = (e) => {
        runInAction(() => {
            this.refreshing = true;
            this.isLoading = true;
            this.segmentedControlIndex = e.nativeEvent.selectedSegmentIndex;
            this.updateRepairList(this.pageIndex);
        });
    }
}

export default new RepairListStor()